<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="巡检类型1、云平台服务器2、海康TIM" prop="xjlx">
        <el-input
          v-model="queryParams.xjlx"
          placeholder="请输入巡检类型1、云平台服务器2、海康TIM"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="巡检时间" prop="xjsj">
        <el-date-picker clearable
          v-model="queryParams.xjsj"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择巡检时间">
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="巡检内容服务器结果" prop="xjnr">
        <el-input
          v-model="queryParams.xjnr"
          placeholder="请输入巡检内容服务器结果"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="巡检结果" prop="xjjg">
        <el-select v-model="queryParams.xjjg" placeholder="请选择巡检结果" clearable>
          <el-option
            v-for="dict in sys_xj_xjjg"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="巡检干系人" prop="xjgxr">
        <el-input
          v-model="queryParams.xjgxr"
          placeholder="请输入巡检干系人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="巡检来源：自动/手动" prop="xjly">
        <el-input
          v-model="queryParams.xjly"
          placeholder="请输入巡检来源：自动/手动"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['manage:xjjl:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:xjjl:edit']"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:xjjl:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['manage:xjjl:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="xjjlList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" type="index"  width="55" />
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="巡检类型" align="center" prop="xjlx" />
      <el-table-column label="巡检时间" align="center" prop="xjsj" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.xjsj, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="巡检内容服务器结果" align="center" prop="xjnr" /> -->
      <el-table-column label="巡检结果" align="center" prop="xjjg">
        <template #default="scope">
          <dict-tag :options="sys_xj_xjjg" :value="scope.row.xjjg"/>
        </template>
      </el-table-column>
      <el-table-column label="巡检干系人" align="center" prop="xjgxr" />
      <el-table-column label="巡检来源" align="center" prop="xjly" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleDetail(scope.row)" v-hasPermi="['manage:xjjl:edit']">查看详情</el-button>
          <!-- <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['manage:xjjl:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['manage:xjjl:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改巡检记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="xjjlRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="巡检类型1、云平台服务器2、海康TIM" prop="xjlx">
          <el-input v-model="form.xjlx" placeholder="请输入巡检类型1、云平台服务器2、海康TIM" />
        </el-form-item>
        <el-form-item label="巡检时间" prop="xjsj">
          <el-date-picker clearable
            v-model="form.xjsj"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择巡检时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="巡检内容服务器结果" prop="xjnr">
          <el-input v-model="form.xjnr" placeholder="请输入巡检内容服务器结果" />
        </el-form-item>
        <el-form-item label="巡检结果1、正常2、警告" prop="xjjg">
          <el-radio-group v-model="form.xjjg">
            <el-radio
              v-for="dict in sys_xj_xjjg"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="巡检干系人" prop="xjgxr">
          <el-input v-model="form.xjgxr" placeholder="请输入巡检干系人" />
        </el-form-item>
        <el-form-item label="巡检来源" prop="xjly">
          <el-input v-model="form.xjly" placeholder="请输入巡检来源" />
        </el-form-item>
        <el-divider content-position="center">${subTable.functionName}信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddXjjlsub">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteXjjlsub">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="xjjlsubList" :row-class-name="rowXjjlsubIndex" @selection-change="handleXjjlsubSelectionChange" ref="xjjlsub">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="设备id" prop="deviceid" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.deviceid" placeholder="请输入设备id" />
            </template>
          </el-table-column>
          <el-table-column label="设备IP" prop="deviceip" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.deviceip" placeholder="请输入设备IP" />
            </template>
          </el-table-column>
          <el-table-column label="巡检内容" prop="xjnr" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.xjnr" placeholder="请输入巡检内容" />
            </template>
          </el-table-column>
          <el-table-column label="容量大小" prop="xjsize" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.xjsize" placeholder="请输入容量大小" />
            </template>
          </el-table-column>
          <el-table-column label="巡检状态" prop="xjzt" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.xjzt" placeholder="请输入巡检状态" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 详情对话框 -->
    <el-dialog :title="viewTitle" v-model="viewOpen" width="1200px" append-to-body>
      <el-form ref="viewXjjlList" :model="viewForm" label-width="auto">
        <!-- <el-form-item label="巡检ID">
          <div v-text="viewForm.id"></div>
        </el-form-item> -->
        <el-form-item label="巡检时间">
          <div v-text="viewForm.xjsj"></div>
        </el-form-item>
        <el-form-item label="巡检类型">
          <div v-text="viewForm.xjlx"></div>
        </el-form-item>
        <el-form-item label="巡检内容">
          <div v-html="viewForm.xjnr"></div>
          <!-- <pre>{{ viewForm.xjnr  }}</pre> -->
        </el-form-item>
        <el-form-item label="巡检结果" prop="xjjg">
          <!-- <el-radio-group v-model="viewForm.xjjg" disabled>
            <el-radio
              v-for="dict in sys_xj_xjjg"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group> -->
          <el-tag v-if="viewForm.xjjg == 1" size="large" type="success">正常</el-tag>
          <el-tag v-if="viewForm.xjjg == 2" size="large" type="danger">异常</el-tag>
        </el-form-item>
        <el-form-item label="巡检干系人" prop="xjgxr">
          <div v-text="viewForm.xjgxr"></div>
        </el-form-item>
        <el-form-item label="巡检来源" prop="xjly">
          <div v-text="viewForm.xjly"></div>
        </el-form-item>
        <el-divider content-position="center">服务器巡检详细信息</el-divider>
        <el-table :data="viewXjjlSubList" :row-class-name="rowXjjlsubIndex">
          <el-table-column label="序号" align="center" prop="index"></el-table-column>
          <el-table-column label="设备IP" align="center" prop="deviceip"></el-table-column>
          <el-table-column label="巡检内容" align="center" prop="xjnr"></el-table-column>
          <el-table-column label="容量大小" align="center" prop="xjsize"></el-table-column>
          <el-table-column label="单位" align="center" prop="xjdw" width="80px"></el-table-column>
          <el-table-column label="预警大小" align="center" prop="yjrl"></el-table-column>
          <el-table-column label="巡检状态" align="center" prop="xjzt"></el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="viewOpen = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>









  </div>
</template>

<script setup name="Xjjl">
import { listXjjl, getXjjl, delXjjl, addXjjl, updateXjjl } from "@/api/manage/xjjl";
import { ref } from "vue";

const { proxy } = getCurrentInstance();
const { sys_xj_xjjg } = proxy.useDict('sys_xj_xjjg');

const xjjlList = ref([]);
const xjjlsubList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedXjjlsub = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    xjlx: null,
    xjsj: null,
    xjnr: null,
    xjjg: null,
    xjgxr: null,
    xjly: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询巡检记录列表 */
function getList() {
  loading.value = true;
  listXjjl(queryParams.value).then(response => {
    xjjlList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    xjlx: null,
    xjsj: null,
    xjnr: null,
    xjjg: null,
    xjgxr: null,
    xjly: null
  };
  xjjlsubList.value = [];
  proxy.resetForm("xjjlRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加巡检记录";
}

const viewOpen = ref(false);
const viewTitle = ref("");
const viewForm = ref({});
const viewXjjlSubList = ref([]);

// 查看巡检细节
function handleDetail(row){
  reset();
  const _id = row.id || ids.value
  getXjjl(_id).then(response => {
    viewForm.value = response.data;
    viewXjjlSubList.value = response.data.xjjlsubList;
    viewOpen.value = true;
    viewTitle.value = "巡检记录";
  });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getXjjl(_id).then(response => {
    form.value = response.data;
    xjjlsubList.value = response.data.xjjlsubList;
    open.value = true;
    title.value = "修改巡检记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["xjjlRef"].validate(valid => {
    if (valid) {
      form.value.xjjlsubList = xjjlsubList.value;
      if (form.value.id != null) {
        updateXjjl(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addXjjl(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除巡检记录编号为"' + _ids + '"的数据项？').then(function() {
    return delXjjl(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** ${subTable.functionName}序号 */
function rowXjjlsubIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** ${subTable.functionName}添加按钮操作 */
function handleAddXjjlsub() {
  let obj = {};
  obj.deviceid = "";
  obj.deviceip = "";
  obj.xjnr = "";
  obj.xjsize = "";
  obj.xjzt = "";
  xjjlsubList.value.push(obj);
}

/** ${subTable.functionName}删除按钮操作 */
function handleDeleteXjjlsub() {
  if (checkedXjjlsub.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的${subTable.functionName}数据");
  } else {
    const xjjlsubs = xjjlsubList.value;
    const checkedXjjlsubs = checkedXjjlsub.value;
    xjjlsubList.value = xjjlsubs.filter(function(item) {
      return checkedXjjlsubs.indexOf(item.index) == -1
    });
  }
}

/** 复选框选中数据 */
function handleXjjlsubSelectionChange(selection) {
  checkedXjjlsub.value = selection.map(item => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/xjjl/export', {
    ...queryParams.value
  }, `xjjl_${new Date().getTime()}.xlsx`)
}

getList();
</script>
